//
//  InsuranceViewController.m
//  carcareIOS
//
//  Created by ileo on 16/7/25.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "InsuranceViewController.h"
#import "InsuranceNormallCell.h"
#import "InsuranceHeadCell.h"
#import "TakePhotoTestViewController.h"
#import "AdverticePopView.h"
#import "NetInsurance.h"
#import "BaseWebViewController.h"
#import "WebViewHandle.h"

#define CELL_HEIGHT (49)

@interface InsuranceViewController()

@property (nonatomic, strong) PolicyModel * policyModel;

@property (nonatomic , strong) UIView * adverticeBackV;//是弹出广告层的背景view
@property (nonatomic , strong) UIView * adverticeBlackBackV;//弹出广告层的黑色的背景view
@property (nonatomic , strong) AdverticePopView * adverticePopV;//弹出的广告view
@property (nonatomic , assign) BOOL adverticePopVBool;//控制动画实现的控制

@property (nonatomic , copy) NSArray * listArr;

@end
@implementation InsuranceViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self reloadDataFinish:nil fail:nil];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.adverticePopVBool = NO;
    [self createAdverticePopView];
    
    if (self.policyModel.status != 4 && self.policyModel.modelDic.count > 0)[self navSetupRightTitle:@"" action:nil];

    
    [self createTableView];
}
#pragma mark -

-(void)loadMoreTopFinish:(void (^)(CGFloat))finish withScrollView:(UIScrollView *)scrollView{
    [self reloadDataFinish:^{
        finish(0);
    } fail:^{
        finish(0);
    }];
}

#pragma mark - 创建弹出的照片
- (void)createAdverticePopView
{
    __weak __typeof(self) wself = self;
    
    UIWindow * mainWindow = [UIApplication sharedApplication].delegate.window;
    [mainWindow addSubview:self.adverticeBackV];
    [self.adverticeBackV addSubview:self.adverticeBlackBackV];
    [self.adverticeBackV addSubview:self.adverticePopV];
    
    [self.adverticeBackV setHidden:YES];
    
    [self.adverticeBlackBackV addTapSingleAction:^(UITapGestureRecognizer *recognizer) {
        [wself adverticePopVBtnClick];
    }];
    
}
#pragma mark - 创建弹出调图片的取消按钮事件
- (void)adverticePopVBtnClick
{
    __weak __typeof(self) wself = self;
    [UIView animateWithDuration:0.5 animations:^{
        [wself.adverticePopV setCenter:(wself.adverticePopVBool ? CGPointMake(__MAIN_WIDTH - 30, 55) : CGPointMake(__MAIN_WIDTH / 2, __MAIN_HEIGHT / 2))];
        if(!wself.adverticePopVBool)[wself.adverticeBackV setHidden: NO];
        if(!wself.adverticePopVBool)[wself.adverticePopV setHidden: NO];
        [wself.adverticePopV setTransform:(wself.adverticePopVBool ? CGAffineTransformMakeScale(0.01, 0.01) : CGAffineTransformMakeScale(1, 1))];
        [wself.adverticeBlackBackV setHidden:wself.adverticePopVBool];
    }completion:^(BOOL finished) {
        [wself.adverticePopV setHidden:wself.adverticePopVBool];
        [wself.adverticeBackV setHidden:wself.adverticePopVBool];
        wself.adverticePopVBool = !wself.adverticePopVBool;
    }];
}
- (void)reloadDataFinish:(void(^)())finish fail:(void(^)())fail;
{
    __weak __typeof(self) wself = self;
    [NetInsurance insurancePolicyStatusFinish:^(PolicyModel *model, NSArray *insureModelArr) {
        wself.policyModel = model;
        wself.listArr = [NSArray arrayWithArray:insureModelArr];
        if (wself.policyModel.status > 3 && [wself.policyModel.imgurl isNotNil]) {
            [wself navSetupRightTitle:@"照片" action:^{
                [UmengMgr event:ICZ_TakePolicyPhotoClick];
                [[StatisticsMgr sharedInstance] addClickID:@"验证通过的保单查看照片" description:@"保险页面右上角照片按钮"];
                [wself adverticePopVBtnClick];
            }];
            [wself.adverticePopV raloadImageUrl:wself.policyModel.imgurl withClick:nil];
        }else [wself navSetupRightTitle:@"" action:nil];
        [wself.tableView reloadData];
        if (finish) {
            finish();
        }

    } fail:fail];
}
- (void)createTableView
{
    self.tableView.frame = CGRectMake(0, 0 , __MAIN_WIDTH , __MAIN_HEIGHT_64_TABBAR );
    self.tableView.backgroundColor = [Theme backGroundColor];
    self.tableView.separatorColor = [UIColor clearColor];
    
}
#pragma mark - tableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.titleArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return section == 2 ? self.listArr.count : [self.titleArray[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    return (indexPath.section == 0 ?  (self.policyModel.status == 4 ? CELL_HEAD_BIG_HEIGHT : CELL_HEAD_SMALL_HEIGHT) :(indexPath.section == 1 ? CELL_HEIGHT : [InsuranceNormallCell getCellHeight]));
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell;
    CGFloat labelX = 22 * __MAIN_RATIO_375;
    switch (indexPath.section) {
        case 0:
        {
            NSString * cellID = @"insuranceHeadCellID";
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[InsuranceHeadCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            }
            
            if (self.policyModel.status != 4) {
                //加载小的cell的值
                
                NSString * imageName = self.policyModel.statusImageName;
                NSString * titleStr = self.policyModel.suggestion;
                [((InsuranceHeadCell *)cell) reloadSmallVIconImage:ImageNamed(imageName) title:titleStr questionBtnClick:^{
                    
                }];
            }else
            {
                __weak __typeof(self) wself = self;
                //加载大的cell 的值
                [((InsuranceHeadCell *)cell) reloadBigWithModel:self.policyModel bottomCellClick:^{
                    //查看保单详细信息
#warning 正式的test改为r
                    BaseWebViewController * webVC = [[[BaseWebViewController alloc]initWithURL:[WebViewHandle url:@"http://test.ichezheng.com/cz/appinsur/www/index.html#/query" addParams:@{@"icz_uid" : @(USERINFO.userID), @"icz_key" : USERINFO.iczkey ,@"session" : USERINFO.session}]]navSetupBackItemWithIdentification:BACK_ICON];

                    // webVC.title = @"查看保单";
                    [wself.navigationController pushViewController:webVC animated:YES];
                }];
            }
        }
            break;
        case 1:
        {
            NSString * cellID = @"titleCellID";
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                UIView * backView = [[[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, CELL_HEIGHT)] resetBackgroundColor:COLOR_HEXSTRING(@"#F2F2F2")] setupOnView:cell];
                backView.layer.borderColor = (indexPath.section == 5 ? [UIColor clearColor] : [Theme lineColor]).CGColor;
                backView.layer.borderWidth = 0.5;
                [[UILabel labelWithFrame:CGRectMake(labelX, 0, __MAIN_WIDTH - labelX * 2, CELL_HEIGHT) font:FONT_DEFAULT_Light(15) text:self.titleArray[indexPath.section][indexPath.row] textColor:[Theme wordColor]] setupOnView:cell];
            }

        }
            break;
        case 2:
        {
            NSString * cellID = @"normallCellID";
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[InsuranceNormallCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            }
            
            ((InsuranceNormallCell *)cell).model = (InsurenceNorModel *)self.listArr[indexPath.row];
            
        }
            break;
        default:
            break;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    switch (indexPath.section) {
        case 0:
        {
            [UmengMgr event:ICZ_ChectPolicy];
            [[StatisticsMgr sharedInstance] addClickID:@"验证保单" description:cell.description];
#warning 正式的test改为r
            if (self.policyModel.canClickHeadCell) {
                BaseWebViewController * webVC = [[[BaseWebViewController alloc]initWithURL:[WebViewHandle url:@"http://test.ichezheng.com/cz/appinsur/www/index.html#/verify" addParams:@{@"icz_uid" : @(USERINFO.userID), @"icz_key" : USERINFO.iczkey}]]navSetupBackItemWithIdentification:BACK_ICON];
//                webVC.title = @"验证保单";
                [self.navigationController pushViewController:webVC animated:YES];
            }
            
        }
            break;
        case 2:
        {
            [UmengMgr event:ICZ_ClickPolicyList];
            [[StatisticsMgr sharedInstance] addClickID:@"下边保险列表点击" description:cell.description];
        }
            break;
            
        default:
            break;
    }
}
- (UIView *)adverticeBlackBackV
{
    if (!_adverticeBlackBackV) {
        _adverticeBlackBackV = [[UIView viewWithFrame:__MAIN_BOUND] resetBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.6]];
    }
    return _adverticeBlackBackV;
}

- (AdverticePopView *)adverticePopV
{
    if (!_adverticePopV) {
        CGFloat popVW = 324 * __MAIN_RATIO_375;
        CGFloat popVH = 415 * __MAIN_RATIO_375;
        _adverticePopV = [[AdverticePopView alloc]initWithFrame:CGRectMake( (__MAIN_WIDTH - popVW) / 2, (__MAIN_HEIGHT - popVH) / 2, popVW , popVH )];
    }
    return _adverticePopV;
}
- (UIView *)adverticeBackV
{
    if(!_adverticeBackV)
    {
        _adverticeBackV = [[UIView viewWithFrame:__MAIN_BOUND] resetBackgroundColor:[UIColor clearColor]];
    }
    return _adverticeBackV;
}
- (NSArray *)titleArray
{
    return @[@[@[@[@"willTestPolicyImage",@"马上去验证保单>>"],@[@"isTestingPolicyImage",@"照片正在验证中,需1-3个工作日"],@[@"isTestingPolicyImage",@"消息正在验证中,需1-3个工作日"],@[@"failTestPolicyImage",@"验证失败 重新验证>>"]]],@[@"根据您的驾驶习惯给您推荐的保险"],@[@"normallCell"]];
}
@end
